//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using OpenLS.Chart.Internals;
using OpenLS.Core.Serialization;

namespace OpenLS.Chart
{
    public class ScatterSeries : Series, INumericValuesProvider, IMarkerOwner, IDataPointOwner, IDataLabelsOwner
    {
        private DataLabels dlbls;
        private readonly DataPoints dPt = new DataPoints();
        private ErrorBars errorBars1;
        private ErrorBars errorBars2;
        private Marker _marker;
        private bool? isSmooth;
        private readonly ObservableCollection<TrendLine> trendLines = new ObservableCollection<TrendLine>();
        private DataSource xValues;
        private NumericDataSource yValues;
        private ExtensionList extLst;


        public Marker Marker
        {
            get { return _marker; }
            set { _marker = value;notifyPropertyChanged("Marker"); }
        }

        public ObservableCollection<TrendLine> TrendLines
        {
            get { return trendLines; }
        }

        public DataSource XValues
        {
            get { return xValues; }
            set { xValues = value; notifyPropertyChanged("XValues"); }
        }

        public NumericDataSource YValues
        {
            get { return yValues; }
            set { yValues = value; notifyPropertyChanged("YValues");}
        }

        public bool? IsSmooth
        {
            get { return isSmooth; }
            set { isSmooth = value; notifyPropertyChanged("IsSmooth");}
        }


        public DataPoints DataPoints
        {
            get { return dPt; }
        }


        public ErrorBars ErrorBars1
        {
            get { return errorBars1; }
            set { errorBars1 = value; notifyPropertyChanged("ErrorBars1"); }
        }

        public ErrorBars ErrorBars2
        {
            get { return errorBars2; }
            set { errorBars2 = value; notifyPropertyChanged("ErrorBars2"); }
        }


        public DataLabels DataLabels
        {
            get { return dlbls; }
            set { dlbls = value; notifyPropertyChanged("DataLabels");}
        }

        internal IEnumerable<NumericValue> XNumericValues
        {
            get
            {
                if (XValues is NumericDataSource)
                {
                    var nds = (NumericDataSource)XValues;
                    return nds.NumericValues;
                }
                    //return ((NumericDataSource) XValues.Values).NumericValues;
                throw new NotImplementedException();
            }
        }

        #region INumericValuesProvider Members

        IEnumerable<NumericValue> INumericValuesProvider.GetValues()
        {
            return YValues.NumericValues;
        }

        event EventHandler INumericValuesProvider.ValueChanged
        {
            add
            {
                throw new NotImplementedException();
                
            }
            remove
            {
                throw new NotImplementedException();
                
            }
        }

        #endregion

        internal override void ReadXmlCore(ReadContext context)
        {
            using (var c = context.Read("ser"))
            {
                ReadXmlBase(c);
                Marker = c.ReadOptionalElement<Marker>("marker");
                DataPoints.Clear();
                while (c.ReaderLocalName == "dPt")
                    DataPoints.Add(c.ReadElement<DataPoint>());
                DataLabels = c.ReadOptionalElement<DataLabels>(DataLabels.xmlName);
                TrendLines.Clear();
                foreach (TrendLine l in c.ReadOptionalWhile<TrendLine>(TrendLine.xmlName))
                    TrendLines.Add(l);
                ErrorBars1 = c.ReadOptionalElement<ErrorBars>(ErrorBars.xmlName);
                ErrorBars2 = c.ReadOptionalElement<ErrorBars>(ErrorBars.xmlName);
                XValues = DataSource.ReadOptionalDataSource("xVal", c);
                YValues = NumericDataSource.ReadOptionalNumericDataSource("yVal", c);
                    //\\ c.ReadOptionalElementAs<NumericDataSource>("yVal");
                IsSmooth = c.ReadOptionalElementNullableBooleanAttribute("smooth");
                extLst = c.ReadOptionalElement<ExtensionList>("extLst");
            }
        }

        internal override void WriteXmlCore(WriteContext context)
        {
            using (WriteContext c = context.Write("ser"))
            {
                WriteXmlBase(c);
                c.WriteOptionalElement(Marker);
                c.WriteOptionalElement(DataLabels);
                foreach (TrendLine l in TrendLines)
                    c.WriteElement(l);
                c.WriteOptionalElement(ErrorBars1);
                c.WriteOptionalElement(ErrorBars2);
                WriteOptionalNumericDataSource("xVal", XValues, c);
                WriteOptionalNumericDataSource("yVal", YValues, c); //\\ c.WriteOptionalElementAs(YValues, "yVal");
                c.WriteOptionalElementBooleanAttribute("smooth", IsSmooth);
                c.WriteOptionalElement(extLst);
            }
        }
    }
}